<?php
require_once 'controllers/controller.php';
require_once 'models/conflit_manager.php';
require_once 'models/entite_historique_manager.php';

class controller_conflit extends Controller
{
	protected $_conflit;
	protected $_entite;
	
	public function __construct() {
		parent::__construct();
		$this->_conflit = new conflit_manager($this->_db);
		$this->_entite = new entite_historique_manager($this->_db);
	}
	
	public function hello() {
		$_SESSION['message'] = 'hello world !';
	}

	public function show() {
		if(!isset($_GET['id'])) {
			header('Location: ?section=conflit&action=show_all');
			exit();
		}
		$conflit = $this->_conflit->get_by_id($_GET['id']);
		$entite = $this->_entite->get_all();
		$nb_batailles = count($conflit->get_batailles());
		$camps = array();
		foreach($conflit->get_batailles() as $b){
			$camps[] = count($b->get_camps());
		}
		//$creations = $this->_entite->get_all_date_creation($_GET['id']);
		//$dissolutions = $this->_entite->get_all_date_dissolution($_GET['id']);
		include 'views/conflit_show.php';
	}
	
	public function show_all() {
		$conflits = $this->_conflit->get_all();
		include 'views/conflit_show_all.php';
		echo "<br><br>";
		$entite = $this->_entite->get_all();
		$nb_batailles = 0;
		$camps = array();
		if(isset($_SESSION['isAdmin']))
			include 'views/conflit_create.php';
	}
	
	public function add() {
		
		if(!isset($_SESSION['isAdmin']) && !isset($_POST['nom'])) {
			header('Location: ?section=conflit&action=show');
			exit();
		}
		
		$id = $this->_conflit->max_id() + 1;
		$e = new conflit(array(
				"idConflit" => $id, 
				"nomConflit" => $_POST['nom'], 
				"wikiConflit" => $_POST['wiki'], 
				"mortsconflit" => $_POST['morts'], 
				"blessesconflit" => $_POST['blesses'],
				"debut" => $_POST['debut'], 
				"fin" => $_POST['fin']));
		$i=1;
		$batailles = array();
		$r = new bataille_manager($this->_db);
		while(isset($_POST['nom_bataille'.$i])){
			$id = $r->max_id() + $i;
			$bataille = new bataille(array(
				"idBataille" => $id, 
				"nomBataille" => $_POST['nom_bataille'.$i], 
				"wikiBataille" => $_POST['wiki_bataille'.$i], 
				"debut" => $_POST['debut_bataille'.$i], 
				"fin" => $_POST['fin_bataille'.$i]));

			$j=1;
			$camps = array();
			$s = new camp_manager($this->_db);
			while(isset($_POST['nom_camp'.$i.'-'.$j])){
				$idc = $s->max_id() + $i*$j;
				$camp = new camp(array(
					"idCamp" => $idc, 
					"nomCamp" => $_POST['nom_camp'.$i.'-'.$j], 
					"wikiCamp" => $_POST['wiki_camp'.$i.'-'.$j], 
					"morts" => $_POST['morts_camp'.$i.'-'.$j], 
					"blesses" => $_POST['blesses_camp'.$i.'-'.$j]));
				$entit = preg_split("/[\s,]+/",$_POST['addR'.$i.'-'.$j]);
				$camp->set_entites($entit);
				$camps[] = $camp;
				$j++;
			}
			$bataille->set_camps($camps);
			$batailles[] = $bataille;
			$i++;
		}
		$e->set_batailles($batailles);
		$this->_conflit->add($e);
		header('Location: ?section=conflit&action=show');
		exit();
	}
	
	public function update() {
		print_r($_POST);
		if(!isset($_SESSION['isAdmin']) && !isset($_POST['nom'])) {
			header('Location: ?section=conflit&action=show');
			exit();
		}
		
		$e = new conflit(array(
				"idConflit" => $_POST['id_conflit'], 
				"nomConflit" => $_POST['nom'], 
				"wikiConflit" => $_POST['wiki'], 
				"mortsconflit" => $_POST['morts'], 
				"blessesconflit" => $_POST['blesses'],
				"debut" => $_POST['debut'], 
				"fin" => $_POST['fin']));
		$i=1;
		$batailles = array();
		$r = new bataille_manager($this->_db);
		while(isset($_POST['nom_bataille'.$i])){
			if(isset($_POST['id_bataille'.$i])) $idb = $_POST['id_bataille'.$i];
			else $idb = $r->max_id() + $i;
			$bataille = new bataille(array(
				"idBataille" => $idb, 
				"nomBataille" => $_POST['nom_bataille'.$i], 
				"wikiBataille" => $_POST['wiki_bataille'.$i], 
				"debut" => $_POST['debut_bataille'.$i], 
				"fin" => $_POST['fin_bataille'.$i]));
			$j=1;
			$camps = array();
			$s = new camp_manager($this->_db);
			while(isset($_POST['nom_camp'.$i.'-'.$j])){
				if(isset($_POST['id_camp'.$i.'-'.$j])) $idc = $_POST['id_camp'.$i.'-'.$j];
				else $idc = $s->max_id() + $i*$j;
				$camp = new camp(array(
					"idCamp" => $idc, 
					"nomCamp" => $_POST['nom_camp'.$i.'-'.$j], 
					"wikiCamp" => $_POST['wiki_camp'.$i.'-'.$j]));
				if(isset($_POST['morts_camp'.$i.'-'.$j]) && isset($_POST['blesses_camp'.$i.'-'.$j])){
					 
					$camp->set_morts($_POST['morts_camp'.$i.'-'.$j]); 
					$camp->set_blesses($_POST['blesses_camp'.$i.'-'.$j]);
				}
				$entit = preg_split("/[\s,]+/",$_POST['addR'.$i.'-'.$j]);
				$camp->set_entites($entit);
				$camps[] = $camp;
				$j++;
			}
			$bataille->set_camps($camps);
			$batailles[] = $bataille;
			$i++;
		}
		$e->set_batailles($batailles);
		$this->_conflit->add_or_update($e);
		header('Location: ?section=conflit&action=show&id='.$e->get_idconflit());
		exit();
	}

	public function delete() {
		if(isset($_SESSION['isAdmin'])){
			$conflit = $this->_conflit->get_by_id($_GET['id']);
			$camp_manager = new camp_manager($this->_db);
			foreach($conflit->get_batailles() as $b){
				foreach($b->get_camps() as $c){
					$camp_manager->delete($c);
				}
			}
			$this->_conflit->delete($conflit);
		}
		header('Location: ?section=conflit&action=show_all');
		exit();
	}

	public function delete_bataille(){
		if(isset($_SESSION['isAdmin']) && isset($_GET['id'])){
			$bataille_manager = new bataille_manager($this->_db);
			$bataille_manager->delete_by_id($_GET['id']);
		}
	}

	public function delete_camp(){
		if(isset($_SESSION['isAdmin']) && isset($_GET['id'])){
			$camp_manager = new camp_manager($this->_db);
			$camp_manager->delete_by_id($_GET['id']);
		}
	}
}
?>
